package com.gzsxy.esjy.sign.service.impl;

import com.alibaba.fastjson.JSON;

import com.gzsxy.esjy.sign.dto.BaseSignProtocolDto;
import com.gzsxy.esjy.sign.exception.SignException;
import com.gzsxy.esjy.sign.service.BaseSignProtocol;
import com.gzsxy.esjy.sign.service.GSSignService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 签名版默认实现
 * @author zhangxiaolong
 * @since 2022/7/15
 */
@Slf4j
@Service("base-GSSignService")
@DependsOn("gsWsChannelService")//循环依赖问题
public class GSSignServiceImpl implements GSSignService {

    private Map<String, BaseSignProtocol> baseProtocolMap;

    public GSSignServiceImpl(ApplicationContext context) {
        this.baseProtocolMap = context.getBeansOfType(BaseSignProtocol.class);
    }

    /**
     * 进行签名
     *
     * @param ip   设备IP
     * @param port 设备端口
     * @param dto  协议实体
     */
    @Override
    public void sign(String ip, Integer port, BaseSignProtocolDto dto) {
        if (StringUtils.isBlank(ip) || null == port){
            throw new SignException("IP OR port 不能为空");
        }

        log.info("sign:{}", JSON.toJSONString(dto));
        //通过bean名称获取对应不同类型的签名板
        BaseSignProtocol baseSignProtocol = baseProtocolMap.get(dto.getDevProtocolHandler());
        //进行签名
        baseSignProtocol.sign(ip, port, dto);
    }

    /**
     * 取消签名
     *
     * @param ip   设备IP
     * @param port 设备端口
     * @param dto  协议实体
     */
    @Override
    public void cancelSign(String ip, Integer port, BaseSignProtocolDto dto) {
        if (StringUtils.isBlank(ip) || null == port){
            throw new SignException("IP OR port 不能为空");
        }
        log.info("sign:{}", JSON.toJSONString(dto));

        BaseSignProtocol baseSignProtocol = baseProtocolMap.get(dto.getDevProtocolHandler());
        baseSignProtocol.cancelSign(ip, port, dto);
    }

    /**
     * 设置设备信息
     *
     * @param ip   设备IP
     * @param port 设备端口
     * @param dto  协议实体
     */
    @Override
    public void setDeviceInfo(String ip, Integer port, BaseSignProtocolDto dto) {
        if (StringUtils.isBlank(ip) || null == port){
            throw new SignException("IP OR port 不能为空");
        }

        BaseSignProtocol baseSignProtocol = baseProtocolMap.get(dto.getDevProtocolHandler());
        baseSignProtocol.setDeviceInfo(ip, port, dto);
    }

    /**
     * 获取签字板设备状态
     *
     * @param ip   设备IP
     * @param port 设备端口
     * @param type
     */
    @Override
    public void getDeviceStatus(String ip, Integer port, Integer type) {
        if (StringUtils.isBlank(ip) || null == port){
            throw new SignException("IP OR port 不能为空");
        }
    }

    /**
     * 设置签字板属性
     *
     * @param ip   设备IP
     * @param port 设备端口
     * @param dto  协议实体
     */
    @Override
    public void setSignDevParam(String ip, Integer port, BaseSignProtocolDto dto) {
        if (StringUtils.isBlank(ip) || null == port){
            throw new SignException("IP OR port 不能为空");
        }

        BaseSignProtocol baseSignProtocol = baseProtocolMap.get(dto.getDevProtocolHandler());
        baseSignProtocol.setSignDevParam(ip, port, dto);
    }
}
